﻿using System.IO;
using UnityEngine;

/// <summary>
/// 全局文件路径管理
/// </summary>
public class PathManager : MonoBehaviour
{
    public static PathManager Instance { get; private set; }

    /// <summary>
    /// 更新ab文件存放路径 Application.persistentDataPath + "/Update/"
    /// </summary>
    public string abPersistentDataPathRoot;

    /// <summary>
    /// 包体内ab文件存放StreamingAssets路径 带/
    /// </summary>
    public string abStreamingAssetsPathRoot;
    
    public void Init()
    {
        Instance = this;

        abPersistentDataPathRoot = Application.persistentDataPath + "/Update/";
        if (!Directory.Exists(abPersistentDataPathRoot))
        {
            Directory.CreateDirectory(abPersistentDataPathRoot);
        }

        abStreamingAssetsPathRoot = PlatformStreamingAssetsPath(Application.platform);
    }

    /// <summary>
    /// 获取平台名
    /// </summary>
    /// <returns>The platform name.</returns>
    /// <param name="rp">Rp.</param>
    public string GetPlatformName(RuntimePlatform rp)
    {
        switch (rp)
        {
            case RuntimePlatform.Android:
                return rp.ToString();
            case RuntimePlatform.IPhonePlayer:
                return "iOS";
            case RuntimePlatform.WindowsEditor:
            case RuntimePlatform.WindowsPlayer:
                return "Windows";
            case RuntimePlatform.LinuxEditor:
            case RuntimePlatform.LinuxPlayer:
                return "Linux";
            case RuntimePlatform.OSXEditor:
            case RuntimePlatform.OSXPlayer:
                return "OSX";
            default:
                Debug.LogError("不支持的平台");
                return "";
        }
    }

    /// <summary>
    /// 是否为编辑器
    /// </summary>
    /// <returns></returns>
    public bool isEditor()
    {
        bool flag = false;
        switch(Application.platform)
        {
            case RuntimePlatform.Android:
            case RuntimePlatform.IPhonePlayer:
            case RuntimePlatform.WindowsPlayer:
            case RuntimePlatform.OSXPlayer:
                flag = false;
                break;
            case RuntimePlatform.OSXEditor:
            case RuntimePlatform.WindowsEditor:
                flag = true;
                break;
        }
        return flag;
    }

    /// <summary>
    /// 各个平台对应的PlatformStreaming文件夹目录
    /// </summary>
    private string PlatformStreamingAssetsPath(RuntimePlatform rp)
    {
        string path = "";
        switch (rp)
        {
            case RuntimePlatform.Android:
                path = "jar:file://" + Application.dataPath + "!/assets/";
                break;
            case RuntimePlatform.IPhonePlayer:
                path = "file:///" + Application.dataPath + "/Raw/";
                break;
            case RuntimePlatform.WindowsEditor:
            case RuntimePlatform.WindowsPlayer:
            case RuntimePlatform.OSXPlayer:
            case RuntimePlatform.OSXEditor:
            case RuntimePlatform.LinuxEditor:
            case RuntimePlatform.LinuxPlayer:
                path = "file:///" + Application.streamingAssetsPath + "/";
                break;
            default:
                Debug.LogError("不支持的平台");
                break;
        }
        return path;
    }
}